#include "Widget.h"
#include "ui_widget.h"
#include<QFileDialog>
#include<QPainter>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化地图元素
    mPMap=new Gamemap(this);
    QString fileName=QFileDialog::getOpenFileName(this,"打开地图","./","*.txt");
    mPMap->InitByFile(fileName);

    mMapPainter = new QPainter(this);//创建painter

    setFixedSize(1024,768);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::DrawMap()
{
    for(int i=0;i<mPMap->mRow;i++)
    {
        for(int j=0;j<mPMap->mCol;j++)
        {
            QString imgUrl;
            switch(mPMap->mPArr[i][j])
            {
                case Road: imgUrl = "://imag/road.png";break;
                case Wall:imgUrl = "://imag/wall.png"; break;
                case Box:imgUrl = "://imag/box.png"; break;
                case Point:imgUrl = "://imag/point.png"; break;
                case InPoint: imgUrl = "://imag/inpoint.png";break;
            }
            mMapPainter->drawImage(QRect(j*63,i*63,63,63),QImage(imgUrl));
        }
    }
}

void Widget::paintEvent(QPaintEvent* event)
{
    mMapPainter->begin(this);//设置画布

    //画背景

    mMapPainter->drawImage(QRect(0,0,1024,768),QImage("://imag/backg.png"));


    //画地图
    DrawMap();


    mMapPainter->end();
}
